﻿namespace EasyWeb
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading;
    using System.Threading.Tasks;

    internal static partial class EWHelpers
    {
        public static class Async
        {
            private static readonly TaskFactory defaultTaskFactory = new TaskFactory(CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler.Default);

            public static TResult RunSync<TResult>(Func<Task<TResult>> func)
            {
                EWDebug.IsNotNull(func, "func");

                return defaultTaskFactory.StartNew(func).Unwrap().GetAwaiter().GetResult();
            }

            public static void RunSync(Func<Task> func)
            {
                EWDebug.IsNotNull(func, "func");

                defaultTaskFactory.StartNew(func).Unwrap().GetAwaiter().GetResult();
            }
        }
    }
}
